package vboot.core.module.sys.api.main;

import vboot.core.common.dao.JdbcDao;
import vboot.core.common.api.R;
import vboot.core.common.dao.Sqler;
import vboot.core.common.pojo.Zinp;
import vboot.core.xutil.lang.XstrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("sys/api/main")
public class SysApiMainApi {

    @GetMapping("tree")
    public R getTree(String id) {
        Sqler sqler = new Sqler("t.id,t.id as name,t.pid","sys_api_main");
        sqler.addLike("t.id", id);
        sqler.addWhere("t.avtag=1");
        List<SysApiMain> list = service.findTree(sqler);
        return R.ok(list);
    }

    @GetMapping
    public R get(String id) {
        Sqler sqler = new Sqler("t.id,t.url,t.type", "sys_api_main");
        sqler.addLike("t.id", id);
        sqler.addOrder("t.crtim");
        sqler.addSelect("t.notes,t.crtim");
        return R.ok(jdbcDao.findPageData(sqler));
    }

    @GetMapping("list")
    public R list(String name) {
        String sql="select * from sys_api_main where avtag=1";
        return R.ok( jdbcDao.findMapList(sql));
    }

    @GetMapping("type")
    public R type(String type) {
        if(XstrUtil.isNotBlank(type)){
            List<Zinp> stores=jdbcDao.findInpList("select id,pid from sys_api_main where pid like ?",type+"%");
            return R.ok(stores);
        }else{
            List<Zinp> stores=jdbcDao.findInpList("select id,pid from sys_api_main");
            return R.ok(stores);
        }
    }

    @GetMapping("one/{id}")
    public R getOne(@PathVariable String id) {
        SysApiMain main=repo.findById(id).get();
        return R.ok(main);
    }

    @PostMapping
    public R post(@RequestBody SysApiMain main) {
        main.setAvtag(true);
//        main.setType("");
        repo.save(main);
        return R.ok();
    }

    @PutMapping
    public R put(@RequestBody SysApiMain main) {
        main.setUptim(new Date());
        repo.save(main);
        return R.ok();
    }

    @DeleteMapping("{ids}")
    public R delete(@PathVariable String[] ids) {
        for (String id : ids) {
            repo.deleteById(id);
        }
        return R.ok();
    }

    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    private SysApiMainRepo repo;

    @Autowired
    private SysApiMainService service;
}
